package com.ebt.m.data.entity;

import java.util.ArrayList;
import java.util.List;

/**
 * 筛选条件
 * 
 * @author Michael.li
 * 
 */
public class WikiSearchInfo {
	/** 年龄区间 */
	public static final String FIELD_AGE = "d_touBaoNianLing_min|d_touBaoNianLing_max";
	/** 保障期间 */
	public static final String FIELD_GUARANTEEPERIOD = "d_baoZhangQiJian";
	/** 缴费期间 */
	public static final String FIELD_PAYMENTPERIOD = "d_jiaoFeiQiJian";
	/** 销售开始期间 */
	public static final String FIELD_SALEDATE = "d_startSellingDate";
	/** 销售渠道 */
	public static final String FIELD_SALECHANNEL = "d_SaleChannelIDs";

	public WikiSearchInfo() {
	}

	public int Id;
	public String Name;
	public String FieldName;
	public int Groups;

	private List<WikiSearchItemInfo> items;

	public List<WikiSearchItemInfo> getItems() {
		if (items == null) {
			items = new ArrayList<WikiSearchItemInfo>();
		}
		return items;
	}

	public void setItems(List<WikiSearchItemInfo> items) {
		this.items = items;
	}

	/**
	 * 投保年龄和销售时间用 '|'分割
	 */
	private String extraValue;

	/**
	 * 投保年龄和销售时间用 '|'分割 返回长度为2的数组
	 * 
	 * @return
	 */
	public String[] getExtraValue() {
		if (extraValue != null) {
			return extraValue.split("\\|", -1);
		} else {
			return new String[] { "", "" };
		}
	}

	/**
	 * 投保年龄和销售时间用 '|'分割
	 * 
	 * @param item
	 */
	public void setExtraValue(String value1, String value2) {
		items = new ArrayList<WikiSearchItemInfo>();
		WikiSearchItemInfo item = new WikiSearchItemInfo();
		item.Checked = true;
		if (FieldName.equals(FIELD_AGE)) {
			value1 = value1.replaceAll("岁", "");
			value1 = value1.length() == 0 ? "0" : value1;

			value2 = value2.replaceAll("岁", "");
			value2 = value2.length() == 0 ? "100" : value2;
			item.Name = value1 + "岁~" + value2 + "岁";
		} else if (FieldName.equals(FIELD_SALEDATE)) {
			item.Name = "×××";
		}
		this.extraValue = value1 + "|" + value2;
		items.add(item);
	}

	/**
	 * 切换选中项目
	 * 
	 * @param selectedItem
	 */
	public void toogleCheckedItem(WikiSearchItemInfo selectedItem) {
		for (WikiSearchItemInfo item : items) {
			if (item.Id == selectedItem.Id) {
				item.Checked = !item.Checked;
				break;
			}
		}
	}

	/**
	 * 设置唯一选中项目
	 * 
	 * @param selectedItem
	 */
	public void setSingleSelectedItem(WikiSearchItemInfo selectedItem) {
		for (WikiSearchItemInfo item : items) {
			if (item.Id == selectedItem.Id) {
				item.Checked = true;
			} else {
				item.Checked = false;
			}
		}
	}

	/**
	 * 获得选中的项目名称
	 * 
	 * @return
	 */
	public String getCheckedName() {
		StringBuilder builder = new StringBuilder();
		boolean first = true;
		for (WikiSearchItemInfo item : items) {
			if (item.Checked) {
				if (!first) {
					builder.append("、");
				}
				builder.append(item.Name);
				first = false;
			}
		}
		return builder.toString();
	}

	/**
	 * 多个值用|分割
	 * 
	 * @return
	 */
	public String getCheckedValue() {
		StringBuilder builder = new StringBuilder();
		boolean first = true;
		for (WikiSearchItemInfo item : items) {
			if (item.Checked) {
				if (!first) {
					builder.append("|");
				}
				builder.append(item.FieldValue);
				first = false;
			}
		}
		return builder.toString();
	}

	/**
	 * 是否有选中项
	 * 
	 * @return
	 */
	public boolean hasChecked() {
		boolean checked = false;
		for (WikiSearchItemInfo item : items) {
			if (item.Checked) {
				checked = true;
				break;
			}
		}
		return checked;
	}

	/**
	 * 清除选中状态
	 */
	public void clearChecked() {
		for (WikiSearchItemInfo item : items) {
			item.Checked = false;
		}
	}

	public List<WikiSearchItemInfo> getPositiveItems() {
		List<WikiSearchItemInfo> list = new ArrayList<WikiSearchItemInfo>();
		for (WikiSearchItemInfo item : items) {
			if (item.FieldValue >= 0) {
				list.add(item);
			}
		}
		return list;
	}

	public List<WikiSearchItemInfo> getNegativeItems() {
		List<WikiSearchItemInfo> list = new ArrayList<WikiSearchItemInfo>();
		for (WikiSearchItemInfo item : items) {
			if (item.FieldValue < 0) {
				list.add(item);
			}
		}
		return list;
	}
}
